package com.bootdo.clouddo.service.book.impl;

import com.bootdo.clouddo.dao.book.BookContentMapper;
import com.bootdo.clouddo.domain.book.BookContent;
import com.bootdo.clouddo.service.book.IBookContentService;
import com.bootdo.clouddo.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BookContentServiceImpl implements IBookContentService {

    @Autowired
    private BookContentMapper dao;

    @Override
    public int save(BookContent record){
        if(record.getBookId() == null || record.getSiteId() == null || StringUtils.isBlank(record.getUrl()) || StringUtils.isBlank(record.getContent())) {
            return 0;
        }
        if(dao.find(record.getBookId(), record.getSiteId(), record.getUrl()) == null) {
            return dao.insertSelective(record);
        }
        return 0;
    }

    @Override
    public int save(Long bookId, Integer siteId, String chapter, String url, String content) {
        if(dao.find(bookId, siteId, url) == null) {
            BookContent record = new BookContent();
            record.setAddTime(new Date());
            record.setBookId(bookId);
            record.setSiteId(siteId);
            record.setChapter(chapter);
            record.setUrl(url);
            record.setContent(content);
            return dao.insertSelective(record);
        }
        return 0;
    }

    @Override
    public BookContent find(Long bookId, Integer siteId, String url){
        return dao.find(bookId, siteId, url);
    }

}
